package com.wlstock.funi.request;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.wlstock.funi.app.FundApplication;
import com.wlstock.funi.utils.HttpUtils;

import java.util.HashMap;
import java.util.Map;


public class CustomStringRequest implements ErrorListener, Listener<String>{
	private String key;
	private String speceifcKey;
	public NetWorkListener networkResponseListener;
	public ParseNetResponseListener parseListener;
	
	public void requestPost(final String host, final String method,final HashMap<String, String> header,final HashMap<String, String> params){
		this.request(Method.POST, host, method, header, params);
	}
	
	public void requestGet(final String host, final String method,final HashMap<String, String> header){
		this.request(Method.GET, host, method, header, null);
	}
	
	public void requestGet(final String host, final String method,final HashMap<String, String> header,final HashMap<String, String> params){
		this.request(Method.GET, host, method, header, params);
	}
	
	private void request(final int requestType, final String host, final String method,final HashMap<String, String> header,final HashMap<String, String> params){
		String requestUrl = "";
		if (requestType == Method.GET && params!=null) {
			requestUrl = host +  HttpUtils.getUrl(method, params);
		}else {
			requestUrl = host + method;
		}
		//以method作为tag
		request(requestType, host, header, params, requestUrl,method);
	}

	public void request(final int requestType, final String host,final HashMap<String, String> header, final HashMap<String, String> params, String requestUrl,String reqTag) {
		//从外部传进来tag作为这个请求的标识
		key = reqTag;
		StringRequest stringRequest = new StringRequest(requestType, requestUrl, this, this){
			
			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				// TODO Auto-generated method stub
				if (header==null) {
					return super.getHeaders();
				}
				if (FundApplication.getInstance().cookieMap.containsKey(host)) {
					String cookie = FundApplication.getInstance().cookieMap.get(host);
					header.put("Cookie", cookie);
				}
				return header;
			}
			
			@Override
			protected Response<String> parseNetworkResponse(
					NetworkResponse response) {
				// TODO Auto-generated method stub
				
				String cookies = response.headers.get("Set-Cookie");
				if (cookies != null && cookies.length() > 0 && cookies.contains("JSESSIONID")) {
					FundApplication.getInstance().cookieMap.put(host, cookies);
				}
				
				if (parseListener!=null) {
					return Response.success(parseListener.parseResponse(response), HttpHeaderParser.parseCacheHeaders(response));
				}else{
					return super.parseNetworkResponse(response);
				}
			}
			
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				// TODO Auto-generated method stub
				if (requestType == Method.GET) {
					return super.getParams();
				} else {
					return params;
				}
			}
		};
		stringRequest.setRetryPolicy(new DefaultRetryPolicy(3000, 0, 1));

		FundApplication.getInstance().getRequestQueue().add(stringRequest);
		FundApplication.getInstance().getRequestQueue().start();
	}
	
	
	@Override
	public void onErrorResponse(VolleyError error) {
		// TODO Auto-generated method stub
		
		networkResponseListener.onErrorResponse(error, key);
	}

	@Override
	public void onResponse(String response) {
		// TODO Auto-generated method stub
		networkResponseListener.onResponse(response, key);
	}

	
}
